﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BTGISWASECO.DataLayer;
using BTGISWASECO.BusinessEntities;
using BTGISWASECO.BusinessLayer;

/// <summary>
/// Summary description for UpdateDataDAL
/// </summary>
/// 
namespace BTGISWASECO.DataLayer
{
    public class UpdateDataDAL
    {
        OngPhanPhoiEntities bk = new OngPhanPhoiEntities();
        public UpdateDataDAL()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public void UseUpdateCursor(ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass, int obj, string IDMaDuongOng, string IDDMA, int HuongDongChay, double ChieuDai)
        {
            
            ESRI.ArcGIS.Geodatabase.IWorkspaceEdit workspaceEdit = null;
            ESRI.ArcGIS.Geodatabase.IDataset aoDataset = featureClass as ESRI.ArcGIS.Geodatabase.IDataset;

            workspaceEdit = aoDataset.Workspace as ESRI.ArcGIS.Geodatabase.IWorkspaceEdit;
            workspaceEdit.StartEditing(false);
            workspaceEdit.StartEditOperation();
            ESRI.ArcGIS.Geodatabase.IQueryFilter queryFilter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
            queryFilter.WhereClause = "OBJECTID =" + obj;
            queryFilter.SubFields = "*"; 
            ESRI.ArcGIS.Geodatabase.IFeatureCursor updateCursor = featureClass.Update(queryFilter, true);
            int IDMaDuongOngIndex = featureClass.FindField("IDMaDuongOng");
            int IDDMAIndex = featureClass.FindField("IDDMA");
            int HuongDongChayIndex = updateCursor.FindField("HuongDongChay");
            int ChieuDaiIndex = featureClass.FindField("ChieuDai");
            ESRI.ArcGIS.Geodatabase.IFeature feature = null;
            try
            {
                while ((feature = updateCursor.NextFeature()) != null)
                {
                    feature.set_Value(IDMaDuongOngIndex,(object)IDMaDuongOng);
                    feature.set_Value(IDDMAIndex, (object)IDDMA);
                    feature.set_Value(HuongDongChayIndex, HuongDongChay);
                    feature.set_Value(ChieuDaiIndex, ChieuDai);
                    updateCursor.UpdateFeature(feature);
                }
            }
            catch (System.Runtime.InteropServices.COMException comExc)
            {
            }

            System.Runtime.InteropServices.Marshal.ReleaseComObject(updateCursor);
            workspaceEdit.StopEditOperation();
            workspaceEdit.StopEditing(true);
        }

        public void UpdateDataSDE(ESRI.ArcGIS.Geodatabase.IWorkspace workspace, int obj, string IDMaDuongOng, string IDDMA, int HuongDongChay, double ChieuDai)
        {
            ESRI.ArcGIS.Server.IServerContext serverContext = null;
            ESRI.ArcGIS.Geodatabase.IWorkspaceEdit workspaceEdit = null;
            ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = null;
            ESRI.ArcGIS.Carto.IFeatureLayer iFeatureLayer = null;
            ESRI.ArcGIS.esriSystem.UID pUID = new ESRI.ArcGIS.esriSystem.UIDClass();
            ESRI.ArcGIS.Geodatabase.IEnumDatasetName EnumDSName;
            ESRI.ArcGIS.Geodatabase.IDatasetName _IDatasetName;

            try
            {

                ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace;
                ESRI.ArcGIS.Geodatabase.IEnumDataset iEnumDataset = workspace.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny);
               // ESRI.ArcGIS.Geodatabase.IEnumDataset dsenum1 = workspace.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureDataset);
               // ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWS = workspace as ESRI.ArcGIS.Geodatabase.IFeatureWorkspace;
                ESRI.ArcGIS.Geodatabase.IEnumDatasetName iEnumDatasetName = workspace.get_DatasetNames(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny);

                ESRI.ArcGIS.Geodatabase.IDatasetName iDatasetName = iEnumDatasetName.Next();

                while (iDatasetName != null)
                {
                    if (iDatasetName.Category == "SDE Feature Class")
                    {
                        if (iDatasetName.Name == "BTGISWASECO.SDE.wOngPhanPhoi")
                        {
                            featureClass = featureWorkspace.OpenFeatureClass(iDatasetName.Name);
                            break;
                        }
                    }
                    else
                        if (iDatasetName.Category == "SDE Feature Dataset")
                        {
                            EnumDSName = iDatasetName.SubsetNames;
                            _IDatasetName = EnumDSName.Next();
                            while (_IDatasetName != null)
                            {
                                if (_IDatasetName.Category == "SDE Feature Class")
                                {
                                    if (_IDatasetName.Name == "BTGISWASECO.SDE.wOngPhanPhoi")
                                    {
                                        featureClass = featureWorkspace.OpenFeatureClass(_IDatasetName.Name);
                                        break;
                                    }
                                }
                                _IDatasetName = EnumDSName.Next();
                            }

                        }
                    iDatasetName = iEnumDatasetName.Next();
                }
                iFeatureLayer = new ESRI.ArcGIS.Carto.FeatureLayer();
                iFeatureLayer.FeatureClass = featureClass;
                ESRI.ArcGIS.Geodatabase.IDataset aoDataset = featureClass as ESRI.ArcGIS.Geodatabase.IDataset;
                workspaceEdit = aoDataset.Workspace as ESRI.ArcGIS.Geodatabase.IWorkspaceEdit;
                workspaceEdit.StartEditing(false);
                workspaceEdit.StartEditOperation();
                ESRI.ArcGIS.Geodatabase.IQueryFilter queryFilter = new ESRI.ArcGIS.Geodatabase.QueryFilterClass();
                queryFilter.WhereClause = "OBJECTID =" + obj;
                queryFilter.SubFields = "*";
                ESRI.ArcGIS.Geodatabase.IFeatureCursor updateCursor = featureClass.Update(queryFilter, true);
                int IDMaDuongOngIndex = featureClass.FindField("IDMaDuongOng");
                int IDDMAIndex = featureClass.FindField("IDDMA");
                int HuongDongChayIndex = updateCursor.FindField("HuongDongChay");
                int ChieuDaiIndex = featureClass.FindField("ChieuDai");
                ESRI.ArcGIS.Geodatabase.IFeature feature = null;
                try
                {
                    while ((feature = updateCursor.NextFeature()) != null)
                    {
                        feature.set_Value(IDMaDuongOngIndex, (object)IDMaDuongOng);
                        feature.set_Value(IDDMAIndex, (object)IDDMA);
                        feature.set_Value(HuongDongChayIndex, HuongDongChay);
                        feature.set_Value(ChieuDaiIndex, ChieuDai);
                        updateCursor.UpdateFeature(feature);
                    }
                }
                catch (System.Runtime.InteropServices.COMException comExc)
                {
                }

                System.Runtime.InteropServices.Marshal.ReleaseComObject(updateCursor);
                workspaceEdit.StopEditOperation();
                workspaceEdit.StopEditing(true);
           }
            catch (System.Exception exception)
            {
                throw exception;
            }
            finally
            {
                if (workspaceEdit != null)
                {
                    if (workspaceEdit.IsBeingEdited())
                        workspaceEdit.StopEditing(false);
                }
                if (serverContext != null)
                    serverContext.ReleaseContext();
            }
        }
    }
}